package cn.stylefeng.guns.modular.wx;

import cn.stylefeng.guns.modular.api.tips.ApiResponseData;
import cn.stylefeng.guns.modular.collect.entity.Collect;
import cn.stylefeng.guns.modular.collect.service.CollectService;
import cn.stylefeng.guns.modular.course.entity.Category;
import cn.stylefeng.guns.modular.course.entity.Course;
import cn.stylefeng.guns.modular.course.entity.CourseVideo;
import cn.stylefeng.guns.modular.course.entity.CustomerCourse;
import cn.stylefeng.guns.modular.course.service.CategoryService;
import cn.stylefeng.guns.modular.course.service.CourseService;
import cn.stylefeng.guns.modular.course.service.CourseVideoService;
import cn.stylefeng.guns.modular.course.service.CustomerCourseService;
import cn.stylefeng.guns.modular.customer.entity.Customer;
import cn.stylefeng.guns.modular.customer.entity.Order;
import cn.stylefeng.guns.modular.customer.entity.Rebate;
import cn.stylefeng.guns.modular.customer.service.CustomerService;
import cn.stylefeng.guns.modular.customer.service.OrderService;
import cn.stylefeng.guns.modular.customer.service.RebateService;
import cn.stylefeng.guns.modular.yy.entity.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@RequestMapping(value = "web/course")
@Controller
public class WxCourseController {
    @Autowired
    CategoryService categoryService;

    @Autowired
    CourseService courseService;

    @Autowired
    CourseVideoService courseVideoService;

    @Autowired
    CustomerCourseService customerCourseService;
    @Autowired
    RebateService rebateService;
    @Autowired
    OrderService orderService;
    @Autowired
    CollectService collectService;

    @Autowired
    CustomerService customerService;

    @Autowired
    HttpSession session;
    private String PREFIX = "/web";




    /*
    课程列表首页
     */


    @RequestMapping("course_list")
    public String courseList(Model model, Integer categoryId) {

        //查询一级分类、二级分类以及二级分类关联的课程
        Category parent = new Category();
        parent.setPid(0);
        List<Category> parentList = categoryService.findListBySpec(parent);
        if (categoryId == null || categoryId == 0) {
            if (parentList.size() > 0) {
                parent = parentList.get(0);
            }
        } else {
            parent = categoryService.getById(categoryId);
        }

        //再查子分类以及子分类的课程
        Category son = new Category();
        son.setPid(parent.getId());
        List<Category> sonList = categoryService.findListBySpec(son);

        for (Category s : sonList) {
            //查询课程
            Course course = new Course();
            course.setCategoryId(s.getId());
            List<Course> courseList = courseService.findListBySpec(course);
            s.setCourseList(courseList);

        }

        model.addAttribute("category", parentList);
        model.addAttribute("list", sonList);
        model.addAttribute("categoryId", parent.getId());


        return PREFIX + "/course/course_list.html";
    }


    /*
    课程页面
     */
    @RequestMapping("course")
    public String index(Model model, Integer id, Integer videoId) {
        Customer customer = (Customer) session.getAttribute("customer");

        Course course = courseService.getById(id);
        Category category = categoryService.getById(course.getCategoryId());
        //课程视频

        CourseVideo courseVideo = new CourseVideo();
        courseVideo.setCourseId(id);
        List<CourseVideo> courseVideoList = courseVideoService.findListBySpec(courseVideo);
        //如果传了videoid显示相关视频，如果没传默认显示第一个视频
        if (videoId == null || videoId == 0) {

            if (courseVideoList.size() > 0) {
                videoId = courseVideoList.get(0).getId();
                model.addAttribute("video", courseVideoList.get(0));
            } else {
                model.addAttribute("video", new CourseVideo());
            }

        } else {
            CourseVideo video = courseVideoService.getById(videoId);

            model.addAttribute("video", video);

        }
        //判断用户有没有购买这个课程,查询是否买了整节课或者买了单节课
        CustomerCourse customerCourse = new CustomerCourse();
        customerCourse.setCourseId(id);
        customerCourse.setVideoId(videoId);
        List<CustomerCourse> customerCourseList = customerCourseService.findListBySpec(customerCourse);
        if (customerCourseList.size() > 0) {
            model.addAttribute("isBuy", "1");
        } else {
            model.addAttribute("isBuy", "0");
        }

        //判断用户是否收藏
        Collect collect=new Collect();
        collect.setCustomerId(customer.getId());
        collect.setCourseId(id);

        List<Collect> collectList=collectService.findListBySpec(collect);
        if(collectList.size()>0) {
            model.addAttribute("collect", 1);
        }else{
            model.addAttribute("collect", 0);
        }

        model.addAttribute("course", course);
        model.addAttribute("category", category);
        model.addAttribute("videoList", courseVideoList);
        return PREFIX + "/course/course.html";
    }

    /*
    收藏课程
     */

    @ResponseBody
    @RequestMapping("collect")
    public ApiResponseData collect(Integer courseId) {
        Customer customer = (Customer) session.getAttribute("customer");
        Collect collect=new Collect();
        collect.setCustomerId(customer.getId());
        collect.setCourseId(courseId);

        List<Collect> collectList=collectService.findListBySpec(collect);
        if(collectList.size()>0){
            collectService.delete(collectList.get(0));
            return ApiResponseData.success("操作成功", 0);
        }else{
            collectService.add(collect);
            return ApiResponseData.success("操作成功", 1);
        }



    }


    /*
    购买课程，单节或者整套
     */
    @ResponseBody
    @RequestMapping("buy")
    public ApiResponseData buy(Integer courseId, Integer videoId) {

        Customer customer = (Customer) session.getAttribute("customer");
        Course course = courseService.getById(courseId);
        //插入订单
        Order order = new Order();
        order.setCourseId(courseId);
        order.setVideoId(videoId);
        if (videoId != null && videoId != 0) {
            //购买单节
            CourseVideo courseVideo = courseVideoService.getById(videoId);
            order.setAmount(courseVideo.getPrice() + "");
        } else {
            order.setAmount(course.getPrice() + "");
        }
        order.setCustomerId(customer.getId());
        orderService.add(order);

        //购买成功插入我的课程
        CustomerCourse customerCourse = new CustomerCourse();
        customerCourse.setVideoId(videoId);
        customerCourse.setCourseId(courseId);
        customerCourse.setCustomerId(customer.getId());
        customerCourse.setOrderId(0);
        customerCourseService.add(customerCourse);


        return ApiResponseData.success("", null);
    }


    /*
测试分销返利，支付回调时候设置
1 如果A不是社长，下级B是社长，C不是社长，那么C给B返利，B不给A返任何东西
2 如果A是社长，B不是社长，C是社长的话B社会么都没有，A有帮扶金，如果C是学员，A有帮扶金，B有积分
3 如果A是社长，B是社长，C不管是什么，要给Bf返利，给A返帮扶金


关系情况：
1 若果A是社长，那么作为A的下级没法再有帮扶领导
2 如果A不是社长，那么可以给A的下级设置一个帮扶领导，A忽然变成社长了，那么值钱的帮扶领导都作废

 */
    @RequestMapping("/testRebate")
    @ResponseBody
    public String testRebate() {
        //订单
        Order order = new Order();
        order.setId(1);
        //以第一个课程为例
        Course course = courseService.getById(1);
        /*
        算出返利，津贴，积分
         */
        //返利
        Double rebate = (course.getPrice().doubleValue() * course.getRebate()) / 100;
        //津贴
        Double allowance = (course.getPrice().doubleValue() * course.getLeaderRebate()) / 100;
        //积分
        Double credit = (course.getPrice().doubleValue() * course.getCredit()) / 100;

        Customer customer = customerService.getById(1);
        if (customer.getGrandId() != null && customer.getGrandId() != 0) {
            //有爷爷
            Customer grand = customerService.getById(customer.getGrandId());
            Customer parent = customerService.getById(customer.getPid());
            if (grand.getRole() == 1) {
                //如果爷爷是社长，判断一下爸爸
                if (parent.getRole() == 1) {
                    //如果爸爸是社长，判断自己
                    if (customer.getRole() == 1) {
                        //如果自己也是社长，给爸爸津贴，爷爷啥都没有
                        //BigDecimal amount, Integer customerId, Integer orderId, Integer buyerId, Integer state
                        Rebate father_rebater = new Rebate(new BigDecimal(allowance), parent.getId(), order.getId(), customer.getId(), 1);
                        rebateService.add(father_rebater);

                    } else {
                        //如果自己是学员，给爷爷津贴，给爸爸返利
                        Rebate father_rebater = new Rebate(new BigDecimal(rebate), parent.getId(), order.getId(), customer.getId(), 1);
                        Rebate grand_rebater = new Rebate(new BigDecimal(allowance), grand.getId(), order.getId(), customer.getId(), 1);
                        rebateService.add(father_rebater);
                        rebateService.add(grand_rebater);
                    }


                } else {
                    //如果爸爸是学员，再看自己是是啥
                    if (customer.getRole() == 1) {
                        //如果自己是社长，爸爸啥都没有，爷爷也啥都没有，这种情况什么都不返

                    } else {
                        //如果自己是学员，爸爸有积分，爷爷什么都没有
                        Rebate father_rebater = new Rebate(new BigDecimal(credit), parent.getId(), order.getId(), customer.getId(), 1);
                        rebateService.add(father_rebater);

                    }

                }


            } else {
                //如果爷爷是学员，看爸爸是什么

                if (parent.getRole() == 1) {
                    //如果爸爸是社长，这里给爸爸帮扶金，爷爷啥都没有
                    Rebate father_rebater = new Rebate(new BigDecimal(allowance), parent.getId(), order.getId(), customer.getId(), 1);
                    rebateService.add(father_rebater);
                } else {
                    //如果爸爸是学员，判断自己是什么
                    if (customer.getRole() == 1) {
                        //自己是社长，爸爸啥都没

                    } else {
                        //自己是学员，给爸爸积分
                        Rebate father_rebater = new Rebate(new BigDecimal(credit), parent.getId(), order.getId(), customer.getId(), 1);
                        rebateService.add(father_rebater);
                    }
                }


            }


        } else {
            if (customer.getPid() != null && customer.getPid() != 0) {
                Customer parent = customerService.getById(customer.getPid());
                //有爸爸，看爸爸是什么
                if (parent.getRole() == 1) {
                    //如果爸爸是社长，给爸爸帮扶金
                    Rebate father_rebater = new Rebate(new BigDecimal(allowance), parent.getId(), order.getId(), customer.getId(), 1);
                    rebateService.add(father_rebater);
                } else {
                    //如果爸爸是学员，就要看自己
                    if (customer.getRole() == 1) {
                        //自己是社长，爸爸啥都没

                    } else {
                        //自己是学员，给爸爸积分
                        Rebate father_rebater = new Rebate(new BigDecimal(credit), parent.getId(), order.getId(), customer.getId(), 1);
                        rebateService.add(father_rebater);

                    }
                }


            }
            //没爸爸啥也不用干

        }


        if (customer.getPid() != null && customer.getPid() != 0) {
            Customer parent = customerService.getById(customer.getPid());
            //说明有上级，开始判断
            if (customer.getRole() == 0) {
                //如果本人是学员
                if (parent.getRole() == 0) {
                    //如果上级是学员，返还积分

                    //判断是否还有更高一级


                } else if (parent.getRole() == 1) {
                    //如果上级是社长，先判断有没有上上级
                    if (customer.getGrandId() != null && customer.getGrandId() != 0) {
                        //如果有上上级
                        Customer grand = customerService.getById(customer.getGrandId());
                        //如果上上级是社长，就给上上级帮扶奖金，如果上上级不是社长，不会给上上级返利
                        if (grand.getRole() == 1) {
                            //这里给上上级帮扶奖金


                            //这里给上级返利
                        } else {
                            //

                        }


                    } else {
                        //如果没有上上级

                    }


                }

            } else {
                //如果本人是社长

            }


        }
        if (customer.getGrandId() != null && customer.getGrandId() != 0) {
            //说明有上上级


        }


        return "success";
    }






}
